<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('分配设备')" />
</head>
<body class="gray-bg">
	<div class="container-div">
		<div class="row">
			<div class="col-sm-12 search-collapse">
				<form id="role-form">
				    <input type="hidden" id="tenantId" name="tenantId"  th:value="${user.userId}">
					<div class="select-list">
						<ul>
							<li>
								设备编号：<input type="text" name="deviceCode"/>
							</li>
							<li>
								<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
							    <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
							</li>
						</ul>
					</div>
				</form>
			</div>
	        
	        <div class="btn-group-sm" id="toolbar" role="group">
				<a class="btn btn-success" onclick="selectDevice()" shiro:hasPermission="system:role:add">
	                <i class="fa fa-window-restore"></i> 选择设备
	            </a>
				<a class="btn btn-danger multiple disabled" onclick="cancelDistributionDeviceAll()" shiro:hasPermission="system:role:remove">
		            <i class="fa fa-remove"></i> 批量取消绑定
		        </a>
		        <a class="btn btn-warning" onclick="closeItem()">
		            <i class="fa fa-reply-all"></i> 关闭
		        </a>
	        </div>
	        
	        <div class="col-sm-12 select-table table-striped">
			    <table id="bootstrap-table"></table>
			</div>
		</div>
	</div>
	<th:block th:include="include :: footer" />
	<script th:inline="javascript">
		var removeFlag = [[${@permission.hasPermi('system:user:remove')}]];
		var prefix = ctx + "biz/partners/distribution/device";

		$(function() {
			var options = {
		        url: prefix + "/allocatedList",
		        queryParams: queryParams,
		        sortName: "createTime",
		        sortOrder: "desc",
		        columns: [{
		            checkbox: true
		        },
		        {
		            field: 'id',
		            title: '设备ID',
		        },
				{
					field: 'deviceCode',
					title: '设备编号'
				},
		        {
		            field: 'createTime',
		            title: '创建时间',
		            sortable: true
		        },
		        {
		            title: '操作',
		            align: 'center',
		            formatter: function(value, row, index) {
		                var actions = [];
		                actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="cancelDistributionDevice(\'' + row.id + '\')"><i class="fa fa-remove"></i>取消绑定</a> ');
		                return actions.join('');
		            }
		        }]
		    };
		    $.table.init(options);
		});
		
		function queryParams(params) {
			var search = $.table.queryParams(params);
			return search;
		}
		
		/* 分配设备-选择设备 */
		function selectDevice() {
			var url = prefix + '/select/' + $("#tenantId").val();
		    $.modal.open("选择设备", url);
		}
		
		/* 分配设备-批量取消绑定 */
		function cancelDistributionDeviceAll() {
		    var rows = $.table.selectFirstColumns();
       		if (rows.length == 0) {
       			$.modal.alertWarning("请至少选择一条记录");
       			return;
       		}
       		$.modal.confirm("确认要解绑选中的" + rows.length + "条数据吗?", function() {
    			var data = {"deviceIds": rows.join() };
    			$.operate.submit(prefix + "/cancelAll", "post", "json", data);
    		});
		}
		
		/* 分配设备-取消绑定 */
		function cancelDistributionDevice(deviceId) {
			$.modal.confirm("确认要解绑该设备吗？", function() {
				$.operate.post(prefix + "/cancel", { "deviceId": deviceId });
		    })
		}
	</script>
</body>
</html>